package com.crm.finance.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.ConsultInfoManage;
import com.crm.model.SysDepartment;
import com.crm.model.SysUser;
import com.crm.service.ConsultInfoManageService;
import com.crm.service.SysDepartmentService;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/financeSchoolBalance")
public class FinanceSchoolBalanceController {
	
	@Autowired
	private ConsultInfoManageService service;
	
	@Autowired
	private SysDepartmentService deptService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request, HttpServletResponse response){
		SysUser user = (SysUser) SessionUtils.get(SessionModel.SESSION_USER);
		String deptId = user.getDepartmentId();
		SysDepartment sysDept = deptService.selectByPrimaryKey(deptId);
		if(sysDept!=null){
		String fullPath = sysDept.getFullPath();
		String depId = "";
		if("0".equals(fullPath)){
			depId = deptId;
		}else if(fullPath.substring(2).indexOf(",") == -1){
			depId = fullPath.substring(2);
		}else{
			depId = fullPath.substring(2, fullPath.substring(2).indexOf(","));
		}
		SysDepartment dept = deptService.selectByPrimaryKey(depId);
		if(dept != null && dept.getType() != null){
			if(dept.getType().intValue() == 1){
				request.setAttribute("type", 1);
			}else if(dept.getType().intValue() == 3){
				request.setAttribute("type", 3);
				request.setAttribute("deptId", dept.getDepartmentId());
				request.setAttribute("deptName", dept.getFullName());
			}
		}
	  }
		return "financeCenter/yueselect";
	}
	
	@RequestMapping("/load")
	public void load(Page<ConsultInfoManage> page, ConsultInfoManage manage, HttpServletRequest request, HttpServletResponse response){
		Page<ConsultInfoManage> list = (Page<ConsultInfoManage>) service.selectFinanceSchoolBalance(page, manage);
		ResponseUtil.resultPage(response, request, list);
	}
	
	@RequestMapping("/getSchoolTotalNum")
	@ResponseBody
	public void getSchoolTotalNum(ConsultInfoManage t, HttpServletRequest request, HttpServletResponse response){
		ConsultInfoManage manage = service.getSchoolTotalNum(t);
		ResponseUtil.result(response, JsonUtil.toJsonStr(manage));
	}
	
	@RequestMapping("/loadPersonal")
	@ResponseBody
	public void loadPersonal(ConsultInfoManage t, HttpServletRequest request, HttpServletResponse response){
		ConsultInfoManage manage = service.loadPersonal(t);
		ResponseUtil.result(response, JsonUtil.toJsonStr(manage));
	}
}
